import { getAllMenu } from '@/api/mixins'
import { convert } from '@/utils/transformData'

/**
 * @description 获取所有菜单
 * */
export default {
  data() {
    return {
      menuList: [],
      fatherList: [],
      sonList: [],
      defaultProps: {
        children: "children",
        label: "name"
      }
    }
  },
  methods: {
    async getAllMenu() {
      const { data, success, message } = await getAllMenu()
      if (success) {
        data.forEach(item => {
          if (item.parentId || item.id === 9) {
            this.sonList.push(item.id + '')
          } else {
            this.fatherList.push(item.id + '')
          }
        })
        if (this.isDisabled) {
          this.menuList = convert(data.map(item => ({ ...item, disabled: true })))
        } else {
          this.menuList = convert(data)
        }
      } else {
        this.$message({ type: 'error', message })
      }
    },
    setChecks(auth) {
      let checks = []
      if (auth === 'all') {
        checks = this.fatherList.concat(this.sonList)
      } else {
        checks = auth.split(',').filter(item => this.sonList.includes(item))
      }
      this.$refs.tree.setCheckedKeys(checks)
    },
    async mixinOpen(prop) {
      await this.open()
      const temp = this.postData || this.detailData
      if (this.opId) {
        let auth = ''
        if (prop) {
          auth = temp[prop].auth
        } else {
          auth = temp.auth
        }
        this.setChecks(auth)
      } else {
        this.setChecks('')
      }
    }
  },
  mounted() {
    this.getAllMenu()
  }
}
